<template>
	<form @submit="form.submit($event,success)" :data-action="`/api/station/save?x.id=${data.id}&x.companyId=${data.companyId}&x.type=${type}`">
		
		<view class="fixAuto bb middle">
			<view class="w100">
				点位名称
			</view>
			<view>
				<input type="text" :disabled="disabled" name="x.name|点位名称" maxlength="20" v-model="data.name"  placeholder="请输入点位名称">
			</view>
		</view>
		
		<view class="fixAuto bb middle " @tap="chooseLocation">
			<view class="w100">
				经纬度
			</view>
			<view class="item">
				<view class="flex">
					<view :class="data.longitude ? 'main' :'gray'">{{data.longitude? data.longitude + ',' + data.latitude:'请到达点位进行定位'}}</view>
					<view class="sIcon-locationFill pl10 gray" v-if="!disabled"></view>
					<input type="text" :disabled="disabled" name="x.latitude|经纬度" :value="data.latitude" style="display: none;"/>
					<input type="text"  :disabled="disabled" name="x.longitude" :value="data.longitude" style="display: none;"/>
				</view>
			</view>
		</view>
		
		<view class="father" v-if="type == 1">
			<view class="fixAuto bb">
				<view class="w100">
					是否自动校准
				</view>
				<view class="item ">
					<view class="flex ">
						<view :class="autoCalibrate == 0 ? 'red' : 'green'">{{autoCalibrateList[autoCalibrate]}}</view>
						<view class="sIcon-arrowRight pl10 gray"></view>
						<input type="number"  :disabled="disabled" name="x.autoCalibrate|是否自动校准" :value="autoCalibrate" style="display: none;"/>
					</view>
				</view>
			</view>
			<picker mode="selector" class="abs op0" :range="autoCalibrateList" @change="autoCalibrate=$event.detail.value">
				<view>请选择</view>
			</picker>
		</view>
		
		<view class="fixAuto bb middle">
			<view class="w100">
				控制级别
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.controlLevel|控制级别|0~50" v-model="data.controlLevel" placeholder="请输入控制级别">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				许可证编号
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.licenseNum|排放口许可证编号|0~100" v-model="data.licenseNum" placeholder="请输入排放口许可证编号">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				监控编码
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.monitorNum|监控编码|0~100" v-model="data.monitorNum" placeholder="请输入监控编码">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				设计排放量
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.emissions|设计排放量|0~30" v-model="data.emissions" placeholder="请输入设计排放量">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				排放依据
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.emissionReference|排放依据" maxlength="500" v-model="data.emissionReference" placeholder="请输入排放依据">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				排气筒高度
			</view>
			<view>
				<input type="number" v-if="!disabled" :disabled="disabled" name="x.height|排气筒高度" v-model="data.height" placeholder="请输入排气筒高度，单位：米">
				<view v-else>
					{{data.height}}米
				</view>
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				烟气采样位置
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.samplingLocation|烟气采样位置|0~200" v-model="data.samplingLocation" placeholder="请输入烟气采样位置">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				流速测量位置
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.velocityMeasurement|流速测量位置|0~200" v-model="data.velocityMeasurement" placeholder="请输入流速测量位置">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				测流截面积
			</view>
			<view>
				<input type="number" v-if="!disabled" :disabled="disabled" name="x.measuringSection|测流位置截面积|amount" v-model="data.measuringSection" placeholder="请输入测流位置截面积，单位：㎡">
				<view v-else>
					{{data.measuringSection}}㎡
				</view>
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				采样方式
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.samplingType|采样方式|0~100" v-model="data.samplingType" placeholder="请输入采样方式">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				输送距离
			</view>
			<view>
				<input type="number" v-if="!disabled" :disabled="disabled" name="x.distance|输送距离|amount" v-model="data.distance" placeholder="请输入输送距离，单位：米">
				<view v-else>
					{{data.distance}}米
				</view>
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w100">
				稀释法稀释比
			</view>
			<view>
				<input type="number"  v-if="!disabled" :disabled="disabled" name="x.dilutionRate|稀释法稀释比" v-model="data.dilutionRate" placeholder="请输入稀释法稀释比，单位：%">
				<view v-else>
					{{data.dilutionRate}}%
				</view>
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w130">
				稀释气流量或压力
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.gasFlow|稀释气流量或压力|0~100" v-model="data.gasFlow" placeholder="请输入稀释气流量或压力">
			</view>
		</view>
		<view class="fixAuto bb middle">
			<view class="w130">
				样品气流量或压力
			</view>
			<view>
				<input type="text"  :disabled="disabled" name="x.samplingPressure|样品气流量或压力|0~100" v-model="data.samplingPressure" placeholder="请输入样品气流量或压力">
			</view>
		</view>
		
		<view class="plr100 pd30" v-if="!disabled">
			<button form-type="submit" class="btn block rds23">
				立即保存
			</button>
		</view>
		
	</form>
	
	
</template>

<script>
	import {Qiniu} from "../../common/upload.js" ;
	export default {
		data() {
			return {
				data : { license : "" , longitude : "" ,companyId : "" , id : ""},
				isCompleted : false ,
				qiniu : new Qiniu() ,
				current : 0 ,
				type : 2 ,
				autoCalibrate : 0 ,
				autoCalibrateList : ["非自动校准","自动校准"],
				disabled : true ,
				role : -1
			}
		},
		
		onLoad(param) {
			this.data.companyId = param.id ;
			this.data.id = param.sid ;
			this.loadData();
			this.role = uni.getStorageSync("role");
			this.disabled = this.role != 2 ;
			if (this.role != 2) {
				uni.setNavigationBarTitle({
					title:"废气排放口基本情况"
				});
			}
		},
		
		methods: {
			
			loadData:function(e){
				if(!this.data.id){
					return ;
				}
				if (this.role == 2) {
					uni.setNavigationBarTitle({
						title:"修改废气点位信息"
					});
				}
				var data = {id:this.data.id} ;
				this.kit.get("/api/station/info",data,res => {
					this.data = res.data ;
				});
			},
			
			toUploadImage:function(key){
				var sourceTypes = ["album", "camera"];
				uni.showActionSheet({
					itemList:["从相册选择","从相机拍照"],
					success : res => {
						uni.chooseImage({
							sourceType:[sourceTypes[res.tapIndex]],
							count:1,
							success: res =>{
								uni.showNavigationBarLoading();
								this.qiniu.upload(res.tempFilePaths[0] , res =>{
									this.$set(this.data,key,res);
									uni.hideNavigationBarLoading();
								});
							}
						});
					}
				});
			},
			
			chooseLocation:function(e){
				if (this.disabled) {
					return ;
				}
				uni.getLocation({
					type:"gcj02",
				    success:  (res) =>{
						console.log(res);
						this.data.latitude = parseFloat(res.latitude).toFixed(5) ;
						this.data.longitude =  parseFloat(res.longitude).toFixed(5);
				    },
					fail : (e)=>{
						console.log(e);
						if (e.errMsg == 'getLocation:fail auth deny') {
							uni.showModal({
								title:'提示',
								content:'获取位置未授权，请先授权获取当前地理位置',
								showCancel:true,
								confirmColor:'#3385ff',
								success : (e)=>{
									if (e.confirm) {
										uni.openSetting({
										  success :(res) => {
											   console.log(res)
											  if (res.authSetting['scope.userLocation']) {
											  	 this.chooseLocation();
											  }
										  }
										});
									}
								}
							});
						}else if(e.errMsg == 'getLocation:fail'){
							uni.showToast({
								title: '定位失败',
								icon : 'none'
							});
						}
					},
				});
			},
			
			success:function(e){
				this.kit.goBack();
				setTimeout(e => {
					uni.showToast({
						title: '添加成功',
						icon : 'success'
					});
				},200);
			}
		}
	}
</script>

<style>
page{
	background-color: #fff;
}
.fixAuto{
	padding: 30upx 10upx;
}
</style>
